package com.zhangxi.springbootwebzhangxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhangxi.springbootwebzhangxi.dao.EmpMapper;
import com.zhangxi.springbootwebzhangxi.pojo.Emp;
import com.zhangxi.springbootwebzhangxi.pojo.PageBean;
import com.zhangxi.springbootwebzhangxi.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: EmpServiceImpl
 * Package: com.zhangxi.springbootwebzhangxi.service.impl
 */
@Service
@Slf4j
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize) {

        PageBean  pageBean = new PageBean();
        pageBean.setTotal(empMapper.Total());
        Integer start = (page - 1) * pageSize; // 计算起始索引 , 公式: (页码-1)*页大小
        pageBean.setRows(empMapper.Rows(start,pageSize));
        return pageBean;
    }

//    @Override
//    public List<Emp> list() {
//        return empMapper.list();
//    }

    @Override
    public PageBean list(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行条件分页查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        //获取查询结果
        Page<Emp> p = (Page<Emp>) empList;
        //封装PageBean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void deleteEmp(List<Integer> ids) {
        empMapper.deleteEmp(ids);
    }

    @Override
    public void insertEmp(Emp emp) {
        //补全数据
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insertEmp(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.findById(id);
    }
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now()); //更新修改时间为当前时间

        empMapper.update(emp);
    }
    @Override
    public Emp login(Emp emp) {
        //调用dao层功能：登录
        Emp loginEmp = empMapper.getByUsernameAndPassword(emp);

        //返回查询结果给Controller
        return loginEmp;
    }

}
